﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nyxwin.Main.Base.ViewModels;
using Microsoft.Phone.Controls;

namespace nyxwin.Main.Base.View
{
    public abstract class PivotItemView : UserControl
    {
        public PivotItemView()
        {
            this.Unloaded += new RoutedEventHandler(PivotItemView_Unloaded);
        }

        void PivotItemView_Unloaded(object sender, RoutedEventArgs e)
        {
            if (PivotViewModel != null)
                PivotViewModel.Unload();
        }

        public void Enter()
        {
            OnEnter();

            if (PivotViewModel != null)
                PivotViewModel.Enter();
        }

        protected virtual void OnEnter() { }

        public void Leave()
        {
            OnLeave();

            if (PivotViewModel != null)
                PivotViewModel.Leave();
        }

        protected virtual void OnLeave() { }

        public abstract PivotItemViewModel PivotViewModel { get; }

        protected void Icon_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            DependencyObject depObj = sender as DependencyObject;

            if (depObj != null)
            {
                ContextMenu cm = ContextMenuService.GetContextMenu(depObj);

                if (cm != null && cm.Parent == null)
                {
                    cm.IsOpen = true;
                }
            }
        }
    }

    public abstract class PivotItemView<T> : PivotItemView where T : PivotItemViewModel, new()
    {
        public PivotItemView()
        {
            if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
            {
                this.Model = new T();
                this.DataContext = this.Model;
            }
        }

        public T Model { get; private set; }
        public override PivotItemViewModel PivotViewModel
        {
            get { return Model; }
        }
    }
}
